package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.SupplierRequestBody;
import com.example.elderlycare.dao.entity.Supplier;
import com.example.elderlycare.service.SupplierService;
import com.example.elderlycare.service.entity.SupplierListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    private final Logger logger = LoggerFactory.getLogger(EquipmentController.class);

    @Resource
    SupplierService supplierService;

    /**
     * querySupplier 根据信息查询供应商
     *
     * @param type      供应类型
     * @param name      供应商
     * @param phone     联系方式
     * @param principal 联系人
     * @param pageSize  每页大小
     * @param current   当前页
     * @return SupplierListResponseBody
     */
    @GetMapping
    public Result<SupplierListResponseBody> querySupplier(
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "principal", required = false) String principal,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);
        params.put("name", name != null ? "%" + name + "%" : null);
        params.put("phone", phone);
        params.put("principal", principal != null ? "%" + principal + "%" : null);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);

        SupplierListResponseBody res = supplierService.querySupplierList(params);
        return Result.success(res);
    }

    /**
     * updateSupplier 更新供应商信息
     *
     * @param req UpdateSupplierRequestBody 更新参数
     * @return null
     */
    @PostMapping
    public Result<?> updateSupplier(@Valid @RequestBody SupplierRequestBody.UpdateSupplierRequestBody req) {
        Supplier.SupplierBuilder supplierBuilder = Supplier.builder().
                id(req.getId()).
                name(req.getName()).
                code(req.getCode()).
                address(req.getAddress()).
                email(req.getEmail()).
                phone(req.getPhone()).
                principal(req.getPrincipal()).
                equipTypeId(req.getEquipTypeId());
        boolean success = supplierService.updateById(supplierBuilder.build());
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("update supplier error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * addSupplier 增加供应商
     *
     * @param req AddSupplierRequestBody
     * @return null
     */
    @PutMapping
    public Result<?> addSupplier(@Valid @RequestBody SupplierRequestBody.AddSupplierRequestBody req) {
        Supplier.SupplierBuilder supplierBuilder = Supplier.builder().
                name(req.getName()).
                code(req.getCode()).
                address(req.getAddress()).
                email(req.getEmail()).
                phone(req.getPhone()).
                principal(req.getPrincipal()).
                equipTypeId(req.getEquipTypeId());
        boolean success = supplierService.save(supplierBuilder.build());
        if (success) {
            return Result.success(null);
        } else {
            logger.warn("add supplier error");
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * deleteSupplier 根据id删除供应商
     *
     * @param ids 供应商id
     * @return null
     */
    @DeleteMapping
    public Result<?> deleteSupplier(@RequestParam("ids") @NotEmpty List<Integer> ids) {
        int count = 0;
        for (Integer id : ids) {
            boolean success = supplierService.removeById(id);
            if (!success) {
                logger.warn("delete supplier error");
                count++;
            }
        }
        if (count == 0) {
            return Result.success(null);
        }
        return Result.error(ResultEnum.DEFAULT_ERROR);
    }
}
